package entities;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Rules {
	
	Configuration cfg = null;
	SessionFactory factory = null;
	ServiceRegistry registry = null;
	
	public void initializeParams(){
		cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml");
		registry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
		factory = cfg.buildSessionFactory(registry);
	}
	
	@SuppressWarnings("rawtypes")
	public void readData(){
		Session session = factory.openSession();
		
		Query query = session.createQuery("from user_data");
		List res = query.list();
		UserData data = null;
		
		for (Iterator it = res.iterator(); it.hasNext();) {
			data = (UserData) it.next();
			//System.out.println("ID:" + data.getUserPK().getUserID());
			System.out.println("Name:" + data.getUserName());
			System.out.println("------------------------------");
		}
	}
	
	public static void main(String[] args) {
		Rules r = new Rules();
		r.initializeParams();
		r.readData();
	}

}
